﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace MAT
{
    class CommonClass
    {
        public void _move(ListView oldRoom, ListView newRoom)
        {
            List<ListViewItem> listItem = new List<ListViewItem>();
            foreach (ListViewItem listAdd in oldRoom.Items)
            {
                if (listAdd.Checked)
                    listItem.Add(listAdd);
            }

            foreach (ListViewItem listRemove in listItem)
            {
                listRemove.Remove();
                newRoom.Items.Add(listRemove);
            }
            foreach (ListViewItem ltv in listItem)
            {
                ltv.Checked = false;
            }
        }

        public void automationConnect(ReportDocument rd)
        {
            TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
            TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
            ConnectionInfo crConnectionInfo = new ConnectionInfo();
            Tables CrTables;

            crConnectionInfo.ServerName = @"THANH-PC\SQLEXPRESS";
            crConnectionInfo.DatabaseName = "CAPSTONE";
            crConnectionInfo.UserID = "sa";
            crConnectionInfo.Password = "thanh123456";

            CrTables = rd.Database.Tables;
            foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
            {
                crtableLogoninfo = CrTable.LogOnInfo;
                crtableLogoninfo.ConnectionInfo = crConnectionInfo;
                CrTable.ApplyLogOnInfo(crtableLogoninfo);
            }
        }

        public void report_Parameter(ReportDocument rd, string textBox_text, string parameterString)
        {
            ParameterFieldDefinitions crParameterFieldDefinitions;
            ParameterFieldDefinition crParameterFieldDefinition;
            ParameterValues crParameterValues = new ParameterValues();
            ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();

            crParameterDiscreteValue.Value = textBox_text;
            crParameterFieldDefinitions = rd.DataDefinition.ParameterFields;
            crParameterFieldDefinition = crParameterFieldDefinitions[parameterString];
            crParameterValues = crParameterFieldDefinition.CurrentValues;

            crParameterValues.Clear();
            crParameterValues.Add(crParameterDiscreteValue);
            crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);
        }

        public string getValidString(string input, int length)
        {
            return input.Substring(0, length);
        }
    }
}
